package com.sneakxy.cloudbase.platform.bean.dto;

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.sneakxy.cloudbase.platform.bean.Permission;
import com.sneakxy.cloudbase.platform.bean.ValidAdd;
import com.sneakxy.cloudbase.platform.bean.ValidEdit;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 权限信息DTO
 * @author 潜行的虚影
 * @see
 */
@Data
@ApiModel("权限信息")
@EqualsAndHashCode
@JsonIgnoreProperties(ignoreUnknown = true)
public class PermissionDTO implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1201778889449051963L;

	@NotBlank(groups = {ValidAdd.class, ValidEdit.class})
	@Size(max=64, groups = {ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("权限ID")
	private String id;

	@Size(max=100, groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("备注")
	private String memo;

	@NotBlank(groups={ValidAdd.class, ValidEdit.class})
	@Size(max=64, groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("权限名称")
	private String name;

	@Size(max=64, groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("上级权限ID")
	private String parentId;

	@NotNull(groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("排序")
	private Integer sort;

	@ApiModelProperty("下级权限")
	private List<PermissionDTO> children;

	public static PermissionDTO from(Permission entity) {
		PermissionDTO result = new PermissionDTO();
		BeanUtils.copyProperties(entity, result);
		return result;
	}
	
	public static List<PermissionDTO> from(List<Permission> list) {
		List<PermissionDTO> result = new ArrayList<>(list.size());
		list.forEach(entity -> {
			result.add(from(entity));
		});
		return result;
	}
	
	public static Permission to(PermissionDTO entity) {
		Permission result = new Permission();
		BeanUtils.copyProperties(entity, result);
		return result;
	}
	
	public static List<Permission> to(List<PermissionDTO> list) {
		List<Permission> result = new ArrayList<>(list.size());
		list.forEach(entity -> {
			result.add(to(entity));
		});
		return result;
	}

}
